from django.db import models
import datetime


# Create your models here.
class Company(models.Model):
	company_name = models.CharField('Company Name', maxlength=200)
	company_created = models.DateTimeField(editable=False)
	company_modified = models.DateTimeField(editable=False)
	company_main_address_1 = models.CharField('Main Address', blank=True, maxlength=250)
	company_main_address_2 = models.CharField('Main Address', blank=True, maxlength=100)
	company_main_city = models.CharField('Main City', blank=True, maxlength=200)
	company_main_state = models.CharField('Main State', blank=True, maxlength=100)
	company_main_zip = models.CharField('Main Zip', blank=True, maxlength=10)
	company_main_email_1 = models.EmailField('Company Email', blank=True)
	company_main_email_2 = models.EmailField('Company Email', blank=True)
	company_main_number = models.PhoneNumberField('Company Main Number', blank=True)
	company_main_fax = models.PhoneNumberField('Company Fax Number', blank=True)
	company_main_website = models.URLField('Company Website', blank=True, verify_exists=True)
	company_slug = models.SlugField(prepopulate_from=("company_name",))
	company_notes = models.TextField('Notes', blank=True)
	
	def save(self):
		if not self.id:
			self.company_created = datetime.date.today()
		self.company_modified = datetime.datetime.today()
		super(Company, self).save()
		
	def get_absolute_url(self):
		return "/%s/" % self.company_slug
	
	def __str__(self):
		return self.company_name
	
	class Admin:
		pass
		list_display = ('company_name', 'company_created', 'company_main_email_1', 'company_main_number', 'company_notes')
		search_fields = ['company_name', 'company_main_email_1', 'company_main_email_2', 'company_main_number', 'company_main_fax', 'company_notes']


class Contact(models.Model):
	first_name = models.CharField(maxlength=100)
	last_name = models.CharField(maxlength=100)
	contact_created = models.DateTimeField(editable=False)
	contact_modified = models.DateTimeField(editable=False)
	business_name = models.CharField(blank=True, maxlength=100)
	title = models.CharField(blank=True, maxlength=100)
	company = models.ManyToManyField(Company)
	main_address_1 = models.CharField('Main Address', blank=True, maxlength=200)
	main_address_2 = models.CharField('Main Address', blank=True, maxlength=200)
	main_city = models.CharField('Main City', blank=True, maxlength=200)
	main_state = models.CharField('Main State', blank=True, maxlength=100)
	main_zip = models.CharField('Main Zip', blank=True, maxlength=10)
	billing_address_1 = models.CharField('Billing Address', blank=True, maxlength=200)
	billing_address_2 = models.CharField('Billing Address', blank=True, maxlength=200)
	billing_city = models.CharField('Billing City', blank=True, maxlength=200)
	billing_state = models.CharField('Billing State', blank=True, maxlength=100)
	billing_zip = models.CharField('Billing Zip', blank=True, maxlength=10)
	shipping_address_1 = models.CharField('Shipping Address', blank=True, maxlength=200)
	shipping_address_2 = models.CharField('Shipping Address', blank=True, maxlength=200)
	shipping_city = models.CharField('Shipping City', blank=True, maxlength=200)
	shipping_state = models.CharField('Shipping State', blank=True, maxlength=100)
	shipping_zip = models.CharField('Shipping Zip', blank=True, maxlength=10)
	email_1 = models.EmailField('Email', blank=True)
	email_2 = models.EmailField('Email', blank=True)
	email_3 = models.EmailField('Email', blank=True)
	home_number = models.PhoneNumberField('Home Number', blank=True)
	mobile_number = models.PhoneNumberField('Mobile Number', blank=True)
	work_number = models.PhoneNumberField('Work Number', blank=True)
	dirrect_extension = models.CharField('Direct Extension', blank=True, maxlength=8)
	fax_number = models.PhoneNumberField('Fax Number', blank=True)
	website_1 = models.URLField('Website', blank=True, verify_exists=True)
	website_2 = models.URLField('Website', blank=True, verify_exists=True)
	contact_slug = models.SlugField(prepopulate_from=("last_name",))
	contact_notes = models.TextField('Notes', blank=True)
	# How to combine the first and last name?
	
	def save(self):
		if not self.id:
			self.contact_created = datetime.date.today()
		self.contact_modified = datetime.datetime.today()
		super(Contact, self).save()
		
	def get_absolute_url(self):
		return "/%s/" % self.contact_slug
	
	def __str__(self):
		return self.last_name
	
	class Admin:
		pass
		list_display = ('last_name', 'last_name', 'contact_created', 'email_1', 'home_number', 'mobile_number', 'website_1', 'contact_notes')
		search_fields = ['first_name', 'last_name', 'contact_created', 'email_1', 'email_2', 'email_3', 'home_number', 'mobile_number', 'website_1', 'website_2', 'contact_notes']